package com.yanqu.road.dao.impl.activity.treasure;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.treasure.UserTreasureActivityData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserTreasureActivityDataDaoImpl extends TempDataObjectDao<UserTreasureActivityData> {

    public Map<Integer, UserTreasureActivityData> getUserActivityDataMap(long userId, List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserTreasureActivityData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_treasure where userId = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserTreasureActivityData userTreasureActivityData = parse(rs);
                dataMap.put(userTreasureActivityData.getActivityId(), userTreasureActivityData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updateUserTreasureActivityDataSql = "update t_u_activity_treasure set routeIndex=?,rewardRound=?,taskProgress=?,useTimes=?,score=? where userId=? and activityId=?";
    @Override
    protected boolean updateData(UserTreasureActivityData userTreasureActivityData) {
        userTreasureActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getRouteIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getRewardRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getTaskProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getUseTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userTreasureActivityData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, userTreasureActivityData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getActivityId()));
        if (getDb().executeNoneQuery(updateUserTreasureActivityDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserTreasureActivityDataSql = "insert into t_u_activity_treasure(userId,activityId,routeIndex,rewardRound,taskProgress,useTimes,score) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserTreasureActivityData userTreasureActivityData) {
        userTreasureActivityData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userTreasureActivityData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getRouteIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getRewardRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getTaskProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, userTreasureActivityData.getUseTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userTreasureActivityData.getScore()));
        if (getDb().executeNoneQuery(insertUserTreasureActivityDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserTreasureActivityData parseData(ResultSet rs) throws SQLException {UserTreasureActivityData userTreasureActivityData = new UserTreasureActivityData();
        userTreasureActivityData.setUserId(rs.getLong("userId"));
        userTreasureActivityData.setActivityId(rs.getInt("activityId"));
        userTreasureActivityData.setRouteIndex(rs.getInt("routeIndex"));
        userTreasureActivityData.setRewardRound(rs.getInt("rewardRound"));
        userTreasureActivityData.setTaskProgress(rs.getInt("taskProgress"));
        userTreasureActivityData.setUseTimes(rs.getInt("useTimes"));
        userTreasureActivityData.setScore(rs.getLong("score"));

        return userTreasureActivityData;
    }
}
